/*******************************************************************
 *
 * Copyright (C), 2018-2018, www.letstrong.cn, All rights reserved.
 *
 * Author      : xiaoyu | xiaoyu_ebox@163.com
 * Create Time : 2018-6-9
 * Version     : v0.1
 * Description : 
 *******************************************************************/
/*----------------------------Head file----------------------------*/
#include "ugui_draw.h"

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
extern UG_GUI *g_ugui;

/*-----------------------------------------------------------------*/
#if (CONFIG_UI_BUFFER != UI_BUFFER_NONE)
void draw_pixel_driver(UG_S16 x, UG_S16 y, UG_COLOR c)
{
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
    UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y * 320 + x) << 1));

	//printf("draw pixel:%u %u %04x\r\n", x, y, c);
    *p_ram = c;
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
#if 0
	UG_U8 *p_ram = g_ugui->g_current_fbuffer + ((y * 320 + x) * 3);
    *p_ram++ = c;
    *p_ram++ = (c >> 8);
	*p_ram   = (c >> 16);
#else // test
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y * 320 + x) * 3));

	*p_ram &= (0xFF000000);
	*p_ram |= (c & 0xFFFFFF);
#endif
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y * 320 + x) << 2));

	*p_ram = c;
#else
#error "please check config of CONFIG_GUI_COLOR"
#endif
}

void draw_line_driver( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c)
{
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y1 * 320 + x1) << 1));
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
	UG_U8 *p_ram = g_ugui->g_current_fbuffer + ((y1 * 320 + x1) * 3);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y1 * 320 + x1) << 2));
#endif

    /* horizontal line */
    if(y1 == y2) {
        for(int x = x1; x <= x2; ++x) {
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
			*p_ram++ = c;
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
			*p_ram++ = c;
		    *p_ram++ = (c >> 8);
			*p_ram++ = (c >> 16);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
			*p_ram++ = c;
#endif
        }
    }
    /* vertical line */
    else if(x1 == x2) {
        for(int y = y1; y <= y2; ++y) {
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
			*p_ram++ = c;
			p_ram += (320-1);
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
			*p_ram++ = c;
			*p_ram++ = (c >> 8);
			*p_ram++ = (c >> 16);
			p_ram += ((320-1) * 3);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
			*p_ram++ = c;
			p_ram += (320-1);
#endif            
        }
    }
}

void draw_frame_driver( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c)
{   
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y1 * 320 + x1) << 1));
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
	UG_U8 *p_ram = (g_ugui->g_current_fbuffer + ((y1 * 320 + x1) * 3));
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
	UG_COLOR *p_ram = (UG_COLOR*)(g_ugui->g_current_fbuffer + ((y1 * 320 + x1) << 2));
#endif

    for(int y = y1; y <= y2; ++y) {
        for(int x = x1; x <= x2; ++x) {
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
			*p_ram++ = c;
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
			*p_ram++ = c;
			*p_ram++ = (c >> 8);
			*p_ram++ = (c >> 16);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
			*p_ram++ = c;
#endif
		}

#if (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
		p_ram += ((320-1-x2+x1) * 3);
#else
		p_ram += (320-1-x2+x1);
#endif
	}
}

bool draw_bmp_driver(UG_S16 xp, UG_S16 yp, UG_BMP* bmp, UG_U8 effect)
{
	static UG_U8  direction = 1;
	static UG_U16 offset = 320;
	static UG_U16 minus = 160;
	UG_U8 *bmp_data = bmp->p;

	if(effect) {
		if(minus > 10)
			minus = (minus * 6) / 9;

		if(direction)
			offset = (offset > minus) ? (offset - minus) : 0;
		else
			offset = (offset < (320-minus)) ? (offset + minus) : 320;
	}
	else {
		offset = 0;
	}

#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
	UG_U8 *p_ram = g_ugui->g_current_fbuffer + ((yp * 320 + xp + offset) << 1);
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
	UG_U8 *p_ram = g_ugui->g_current_fbuffer + ((yp * 320 + xp + offset) * 3);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
	UG_U8 *p_ram = g_ugui->g_current_fbuffer + ((yp * 320 + xp + offset) << 2);
#endif

	printf("draw bmp:%d %d %d %d %d\r\n", xp, yp, bmp->width, bmp->height, offset);

	for(UG_S16 y = 0; y < bmp->height; ++y) {
		for(UG_S16 x = 0; x < (bmp->width - offset); ++x) {
#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
			*p_ram++ = *bmp_data++;
#endif
		}

#if (CONFIG_GUI_COLOR == USE_COLOR_RGB565)
		p_ram += ((320-(bmp->width - offset)) << 1);
		bmp_data += (offset << 1);
#elif (CONFIG_GUI_COLOR == USE_COLOR_RGB888)
		p_ram += ((320-(bmp->width - offset)) * 3);
		bmp_data += (offset * 3);
#elif (CONFIG_GUI_COLOR == USE_COLOR_ARGB8888)
		p_ram += ((320-(bmp->width - offset)) << 2);
		bmp_data += (offset << 2);
#endif
	}


#if 1
	if(offset == 0) {
		offset = 320;
		minus = 160;
		return true;
	}

	return false;
#else
	if(direction == 1 && offset == 0) {
		direction = 0;
		minus = 160;
	}
	else if(direction == 0 && offset == 320) {
		direction = 1;
		minus = 160;
	}

	return false;
#endif
}
#endif

